/********************************************************
 Name          : sleep_s.c
 Author        : Khachatur Gyozalyan
 Copyright     : Copyright by Antel Systems
 Description   :
 **********************************************************/
// Include Files
#include "FreeRTOS.h"
#include "task.h"
#include "sleep_s.h"
#include "AntelCfgOID.h"
#include "power_clocks_lib.h"
#include "LMX2541.h"
#include "CMX7164.h"
#include "CMX998.h"
#include "CMX994.h"
#include "tc_1.h"
#include "CfgParams.h"
#include "gpio.h"

int Sleep_state = 0;
Timer_Tik Sleep_TimeOut = 0;
Timer_Tik Sleep_Quiet = 0;
int DSR_old_state = 0;
int DSR_new_state = 0;



/*================================================================================*\
  Author      :       Khachatur Gyozalyan
  ================================================================================
  Function    :       sleep_mode_init
  Description :       Initialize sleep mode parameters.
  Return      :       void
  Parameters  :       void
  Note        :
\*================================================================================*/
void sleep_mode_init(void)
{
	Sleep_TimeOut = TC1_Get_us();
	Sleep_Quiet = TC1_Get_us();
	DSR_old_state = gpio_get_pin_value(USART1_DSR_PIN);
	Sleep_state = 0;
}


/*================================================================================*\
  Author      :       Khachatur Gyozalyan
  ================================================================================
  Function    :       go_static_sleep
  Description :       System goes to static sleep mode.
  Return      :       void
  Parameters  :       void
  Note        :
\*================================================================================*/
void go_static_sleep(void)
{
	LMX2541_Sleep_Wake(0);
	CMX998_Write(0x02 , 0x20);
	CMX7164_RX_Enable(0);
	CMX7164_Tx_Enable(0);
	CMX7164_IDLE();
	CMX994_Sleep();
	//SLEEP(AVR32_PM_SMODE_IDLE);
}

/*================================================================================*\
  Author      :       Khachatur Gyozalyan
  ================================================================================
  Function    :       go_deep_sleep
  Description :       System goes to deep sleep mode.
  Return      :       void
  Parameters  :       void
  Note        :
\*================================================================================*/
void go_deep_sleep(void)
{
	//SLEEP(AVR32_PM_SMODE_STATIC);
}

void wake_up(void)
{
	LMX2541_Sleep_Wake(1);
	CMX7164_RX_Enable(1);
	CMX7164_Tx_Enable(1);
	CMX994_Wake();
	CMX7164_Reinitialize();
	Sleep_state = 0;
}

void wake_if(void)
{
	if(Sleep_state == 0)
		return;
	wake_up();
}

int sleep_task(void)
{
	Timer_Tik tick_tmp;
	if((ConfigRoot.G_Oper.SG_SleepMode.param_sleep_reg & 0x01) == 0)
	{
		if(Sleep_state != 0)
			wake_up();
		return -1;
	}
	if(Sleep_state == 0)
	{
		tick_tmp = ConfigRoot.G_Oper.SG_SleepMode.param_quit;
		if(TC1_IF_us(Sleep_Quiet,tick_tmp*100000))
		{
			go_static_sleep();
			Sleep_TimeOut = TC1_Get_us();
			Sleep_state = ConfigRoot.G_Oper.SG_SleepMode.param_sleep_reg >> 1;
			DSR_old_state = gpio_get_pin_value(USART1_DSR_PIN);
		}
	}
	if(Sleep_state & 0x01)//Wake-up by Timer
	{
		tick_tmp = ConfigRoot.G_Oper.SG_SleepMode.param_wakeup;
		if(TC1_IF_us(Sleep_TimeOut,tick_tmp*100000))
		{
			wake_up();
			Sleep_Quiet = TC1_Get_us();
			return 1;
		}
	}
	if(Sleep_state & 0x02)//Wake-up by toggling on DTR (Data Terminal Ready) input control line
	{
		DSR_new_state = gpio_get_pin_value(USART1_DSR_PIN);
		if(DSR_old_state != DSR_new_state)
		{
			wake_up();
			Sleep_Quiet = TC1_Get_us();
			return 1;
		}
	}
	if(Sleep_state & 0x04)//Wake-up by USB port
	{
		wake_up();
		Sleep_Quiet = TC1_Get_us();
		return 1;
	}
	return 0;
}
